﻿//********************************************************************
// Author:      刘艳明
// Create Date: 2012-08-26
// Description: 
//
// Version:     1.0.0.0
// Change History:
// Date		    Who		      Changes Made
//--------------------------------------------------------------------
// Please Insert the Modiy record here!
//--------------------------------------------------------------------
//********************************************************************


using System;
using System.Collections.Generic;
using System.Data;

using MillerApp.DataAccess;
using MillerApp.DataContract.MillerDB;
using Npgsql;
using NpgsqlTypes;

namespace MillerApp.DataAccess.MillerDB
{
	/// <summary>
	/// 这个类提供了对Usersecurity ()表的数据库操作
	/// </summary>
	public static partial class UsersecurityManager
	{
		private static DbHelper db = DbHelper.CreateDatabase();

		static string insertSQL ="INSERT INTO usersecurity (stockexchangecode,secshortname,createtime,seccode,userid) VALUES (@stockexchangecode,@secshortname,@createtime,@seccode,@userid)";
	
		static string updateSQL ="Update usersecurity Set stockexchangecode=@stockexchangecode,secshortname=@secshortname,createtime=@createtime,seccode=@seccode,userid=@userid Where id=@id,";

		static string deleteSQL ="DELETE FROM  usersecurity  Where id=@id,";	
		
		#region codesmith生成的代码
		/// <summary>
		/// 获取UsersecurityEntity实体 
		/// </summary>
		/// <param name="id">id</param>
		/// <returns>UsersecurityEntity</returns>
		public static UsersecurityEntity GetUsersecurityEntity(int id)
		{
			string strSQL = "select * from usersecurity where id=@id";		
			IDbDataParameter para_id = new NpgsqlParameter("id", NpgsqlDbType.Integer);
			para_id.Value = id;
			IDbDataParameter[] paras=new IDbDataParameter[]{para_id};
			
			
			UsersecurityEntity entity=new UsersecurityEntity();
			using(System.Data.IDataReader reader=db.ExecuteReader(strSQL,paras))
			{
				if(reader.Read()==false) return null;
				LoadFromReader(reader, ref entity);
				return entity;
			}
		}		
		
		/// <summary>
		/// 更新UsersecurityEntity
		/// </summary>
		/// <param name="entity">Entity</param>	
		public static void UpdateUsersecurity(UsersecurityEntity entity)
		{
			if(entity==null)
			{
				throw new Exception("参数[entity]为[null]！");
			}
			IDbDataParameter[] paras =GenerateParas(entity);		
			db.ExecuteNonQuery(updateSQL,paras);
		}
		
		
				
        
        /// <summary>
		///  插入一条记录
		/// </summary>
		/// <param name="id">id</param>		
		public static void InsertUsersecurity(UsersecurityEntity entity)
		{
			if (entity == null)
            {
                throw new Exception("参数[entity]为[null]！");
            }
            IDbDataParameter[] paras = GenerateParas(entity);
            db.ExecuteNonQuery(insertSQL, paras);
		}
		
		/// <summary>
		///  删除一条记录
		/// </summary>
		/// <param name="id">id</param>		
		public static void DeleteUsersecurity(int id)
		{
			IDbDataParameter para_id = new NpgsqlParameter("id", NpgsqlDbType.Integer);
			para_id.Value = id;
			IDbDataParameter[] paras=new IDbDataParameter[]{para_id};
			db.ExecuteNonQuery(deleteSQL,paras);
		}

	
		
		/// <summary>
		/// 删除多条记录
		/// </summary>
		/// <param name="ids">ids</param>		
		public static void DeleteUsersecurity(List<int> ids)
		{
			string strSQL = "DELETE FROM  usersecurity  Where id in ({0}) ";
            string deleteIds = DbHelper.ConvertListToSQLString<int>(ids);
            db.ExecuteNonQuery(string.Format(strSQL, deleteIds),null);
		}
		
		
	
		/// <summary>
		/// 根据分页条件获取Usersecurity数据集
		/// </summary>
		/// <param name="conditions">条件</param>	
		/// <param name="sortExpression">排序表达式，为空则默认为:id</param>		
		/// <param name="pageIndex">开始页码：分页查询中的开始页码</param>
		/// <param name="pageSize">页宽：分页查询中的每页的记录数</param>
		/// <param name="recordCount">页宽：分页查询中的每页的记录数</param>		
		/// <returns>数据集(List)</returns>
		public static List<UsersecurityEntity> GetUsersecuritysByCond(string conditions,string sortExpression,int pageIndex,int pageSize,out int recordCount)
		{
			string viewName="usersecurity";
			return db.GetDataListByCond<UsersecurityEntity>
                (viewName, conditions, sortExpression, pageIndex, pageSize, out recordCount, LoadFromReader);
		}
				
		
		/// <summary>
		/// 根据条件获取Usersecurity数据集
		/// </summary>
		/// <param name="fields">查询的列(如果为空，则表示查询所有列)</param>	
		/// <param name="conditions">条件</param>	
		/// <param name="sortExpression">排序表达式，为空则默认为:id</param>		
		/// <returns>数据集(DataTable)：<br/></returns>
		public static DataTable GetUsersecuritysByCond(string fields,string conditions,string sortExpression)
		{		
			string viewName="usersecurity";	
			return db.GetDataTableByCond(viewName, fields, conditions, sortExpression);		
		}
		#region private method
		/// <summary>
        /// 获取UsersecurityEntity实体数据更新参数
        /// </summary>
        /// <param name="entity">CompanyEntity</param>
        /// <returns></returns>
        private static IDbDataParameter[] GenerateParas(UsersecurityEntity entity)
        { 
			IDbDataParameter para_id = new NpgsqlParameter("id", NpgsqlDbType.Integer);
			para_id.Value = entity.id;
			
			IDbDataParameter para_stockexchangecode = new NpgsqlParameter("stockexchangecode", NpgsqlDbType.Varchar);
			para_stockexchangecode.Value = entity.stockexchangecode;
			
			IDbDataParameter para_secshortname = new NpgsqlParameter("secshortname", NpgsqlDbType.Varchar);
			para_secshortname.Value = entity.secshortname;
			
			IDbDataParameter para_createtime = new NpgsqlParameter("createtime", NpgsqlDbType.Timestamp);
			if (entity.createtime.HasValue)
			{
				para_createtime.Value = DateTime.Parse(string.Format(entity.createtime.Value.ToString(), "yyyy-MM-dd HH:mm:ss"));
			}
			else
			{
				para_createtime.Value = null;
			}
			
			IDbDataParameter para_seccode = new NpgsqlParameter("seccode", NpgsqlDbType.Varchar);
			para_seccode.Value = entity.seccode;
			
			IDbDataParameter para_userid = new NpgsqlParameter("userid", NpgsqlDbType.Varchar);
			para_userid.Value = entity.userid;
			
			return new IDbDataParameter[]{para_id,para_stockexchangecode,para_secshortname,para_createtime,para_seccode,para_userid};	
        }
		
		/// <summary>
		/// 绑定数据到业务实体
		/// </summary>
		/// <param name="reader">DataReader</param>
		/// <param name="entity">业务实体</param>
		private static void LoadFromReader(IDataReader reader,ref UsersecurityEntity entity)
		{			
			if (reader != null && !reader.IsClosed)
			{
				//entity.Id = reader.GetInt32(0);
                if(!Convert.IsDBNull(reader["id"]))entity.id = (int)reader["id"];
                if(!Convert.IsDBNull(reader["stockexchangecode"]))entity.stockexchangecode = reader["stockexchangecode"].ToString();
                if(!Convert.IsDBNull(reader["secshortname"]))entity.secshortname = reader["secshortname"].ToString();
                if(!Convert.IsDBNull(reader["createtime"]))entity.createtime = (DateTime)reader["createtime"];
                if(!Convert.IsDBNull(reader["seccode"]))entity.seccode = reader["seccode"].ToString();
                if(!Convert.IsDBNull(reader["userid"]))entity.userid = reader["userid"].ToString();
			}
			
		}
		#endregion	
		
		#endregion
		
		#region 手工增加的代码
       
        #endregion
	}
}
